package com.laizhenghua.hotel.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.laizhenghua.hotel.utils.PageUtils;
import com.laizhenghua.hotel.utils.R;
import com.laizhenghua.hotel.vo.CatesVo;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.laizhenghua.hotel.entity.CatesEntity;
import com.laizhenghua.hotel.service.CatesService;



/**
 * 
 *
 * @author laizhenghua
 * @email 3299447929@qq.com
 * @date 2021-03-11 15:23:23
 */
@Controller
@RequestMapping("hotel/cates")
public class CatesController {
    @Autowired
    private CatesService catesService;
    /**
     * 更新
     */
    @PostMapping("/cates/update")
    @ResponseBody
    public R updateCates(@RequestBody Map<String,Object> params){
        boolean isUpdate = catesService.updateCates(params);
        Map<String,Object> map = new HashMap<>();
        if(isUpdate){
            map.put("state","success");
            map.put("url","hotel/cates/cates/list");
            map.put("message","修改成功");
        }else {
            map.put("state","error");
            map.put("message","修改失败");
            map.put("url","hotel/cates/cates/list");
        }
        return R.ok().put("data",map);
    }
    /**
     * 跳转至编辑
     */
    @GetMapping("/cates/edit")
    public String toCatesEditPage(){
        return "admin/cates/edit";
    }
    /**
     * 获取更新数据
     */
    @GetMapping("/cates/get_update_data/{catesId}")
    @ResponseBody
    public R getEditData(@PathVariable("catesId") Integer catesId){
        // System.out.println("catesId: "+catesId);
        CatesEntity catesEntity = catesService.getById(catesId);
        return R.ok().put("cates",catesEntity);
    }
    /**
     * 跳转至后台添加页面
     */
    @GetMapping("/cates/add")
    public String toCatesAddPage(){
        return "admin/cates/add";
    }
    /**
     * 跳转至菜单列表
     */
    @GetMapping("/cates/list")
    public String toCatesListPage(Model model){
        List<CatesEntity> list = catesService.list();
        model.addAttribute("list",list);
        return "admin/cates/list";
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = catesService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Integer id){
		CatesEntity cates = catesService.getById(id);

        return R.ok().put("cates", cates);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody CatesEntity cates){
		catesService.save(cates);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody CatesEntity cates){
		catesService.updateById(cates);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody Integer[] ids){
		catesService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
